$(function(){
    
    var ajaxFill = new Z.AjaxFill({
        url: Z.url('/backend/jobs/records',true),
        templates: {
            list: $('#list-template'),
            none: '<tr><td colspan="5" style="text-align:center;">暂无数据</td></tr>'
        },
        data : {
            pageSize : 10,
            page     : 1
        },
        containers:{
            list: $('table.records tbody'),
            pageLinks: $('table.records tfoot td')
        }
    }),
    getId = function(self){
        return Z.parseInt($(self).parent().siblings().first().children(':checkbox').val());
    };
    
    ajaxFill.request();

    $('a.add').click(function(){
        var data = {
            action: 'insert'
        };
        Z.form({
            width: 600,
            content : $('#form-template').tmpl(data),
            success: function(){
                ajaxFill.reload();
            }
        });
        return false;
    });
    
    $('a.edit').live('click',function(){
        var id = getId($(this));
        Z.ajax({
            url : Z.url('/backend/jobs/find/'+id,true),
            success: function(data){
                data.action = 'update';
                Z.form({
                    width: 600,
                    content : $('#form-template').tmpl(data),
                    success: function(){
                        ajaxFill.reload();
                    }
                });
            }
        });
    });

    $('a.delete').live('click',function(){
        var id = getId($(this));
        Z.confirm({
            content: '确定要删除吗？',
            onConfirm: function(){
                Z.ajax({
                    url: Z.url('/backend/jobs/delete/'+id,true),
                    success: function(){
                        ajaxFill.reload();
                    }
                });
            }
        });
        return false;
    });
    
    $('a.show').live('click',function(){
        var id = getId($(this));
        Z.ajax({
            url : Z.url('/backend/jobs/find/'+id,true),
            success: function(data){
                Z.alert({
                    width: 720,
                    content : $('#show-template').tmpl(data,{
                        nl2br : function(s){
                            return s.replace(/\n/g,'<br/>');
                        }
                    })
                });
            }
        });
    });
});